package com.ruoyi.app.dubbo;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.app.api.RemoteAppUserService;
import com.ruoyi.app.api.domain.AppUser;
import com.ruoyi.app.mapper.AppUserMapper;
import com.ruoyi.common.core.enums.UserStatus;
import com.ruoyi.common.core.exception.user.UserException;
import lombok.RequiredArgsConstructor;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;

/**
 * 用户服务
 *
 * @author Lion Li
 */
@RequiredArgsConstructor
@Service
@DubboService
public class RemoteAppUserServiceImpl implements RemoteAppUserService {

    private final AppUserMapper appUserMapper;

    /**
     * 根据手机号查询app用户
     * @param phone
     * @return
     * @throws UserException
     */
    @Override
    public AppUser getAppUserByPhone(String phone) throws UserException {
        AppUser appUser = appUserMapper.selectOne(new LambdaQueryWrapper<AppUser>()
                .eq(AppUser::getPhoneNumber, phone));

        if (ObjectUtil.isNull(appUser)) {
            throw new UserException("用户不存在", phone);
        }
        if (UserStatus.DISABLE.getCode().equals(appUser.getStatus())) {
            throw new UserException("用户状态已禁用", phone);
        }

        // 框架登录不限制从什么表查询 只要最终构建出 LoginUser 即可
        // 此处可根据登录用户的数据不同 自行创建 loginUser 属性不够用继承扩展就行了
        return appUser;
    }
}
